package com.junqson.je.common.model;

import lombok.Getter;

/**
 * @Descriptions
 * @Company
 * @Author Junqson
 * @Date 2020/1/20 10:23
 * @Version 1.0
 */
@Getter
public class ApiResult<T> {
    private int code;

    private String msg;

    private boolean success;

    private T data;

    public ApiResult(int code, String msg, boolean success) {
        this.code = code;
        this.msg = msg;
        this.success = success;
    }

    public ApiResult(int code, String msg, boolean success, T data) {
        this.code = code;
        this.msg = msg;
        this.success = success;
        this.data = data;
    }

    public ApiResult() {
    }

    public static ApiResult error(ResultEnum resultEnum) {
        return new ApiResult(resultEnum.getCode(), resultEnum.getMsg(), false);
    }

    public static ApiResult error(int code, String msg) {
        return new ApiResult<>(code, msg, false);
    }

    public static <T>ApiResult success(T data) {
        return new ApiResult<T>(0, "success", true, data);
    }

    public static ApiResult success() {
        return new ApiResult<>(0, "success", true, null);
    }

    public static ApiResult error(String message) {
        return new ApiResult<>(1, message, false, null);
    }

    public static ApiResult error() {
        return new ApiResult<>(1, "system error", false, null);
    }

    public ApiResult setCode(Integer code) {
        this.code = code;
        return this;
    }

    public ApiResult setData(T data) {
        this.data = data;
        return this;
    }

    public ApiResult setMsg(String msg) {
        this.msg = msg;
        return this;
    }

}
